import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
// import componentsRouter from './modules/components'
// import chartsRouter from './modules/charts'
// import tableRouter from './modules/table'
// import nestedRouter from './modules/nested'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  // {
  //   path: '/aaa',
  //   component: Layout,
  //   meta: {
  //     title: '运营管理',
  //     icon: 'el-icon-s-help',
  //     roles: ['admin', 'editor']
  //   },
  // },
  {
    path: '/permission',
    component: Layout,
    redirect: '/permission/page',
    alwaysShow: true, // will always show the root menu
    name: 'Permission',
    meta: {
      title: '运营中心',
      icon: 'el-icon-shopping-cart-2'
    },
    children: [
      {
        path: 'page',
        component: () => import('@/views/permission/page'),
        name: 'PagePermission',
        meta: {
          title: '商品管理'
        }
      },
      {
        path: 'page/advert',
        component: () => import('@/views/permission/advert'),
        name: 'Advert',
        meta: {
          title: '广告管理',
        }
      },
      {
        path: 'page/addProduct',
        component: () => import('@/views/permission/addProduct'),
        name: 'AddProduct',
        hidden: true,
        meta: {
          title: '新增商品',
        }
      },
      {
        path: 'page/editProduct',
        component: () => import('@/views/permission/addProduct'),
        name: 'EditProduct',
        hidden: true,
        meta: {
          title: '编辑商品',
        }
      },
      // {
      //   path: 'directive',
      //   component: () => import('@/views/permission/directive'),
      //   name: 'DirectivePermission',
      //   meta: {
      //     title: '设备管理',
      //     icon: 'excel'
      //   }
      // }
    ]
  },

  /** when your routing map is too long, you can split it into small modules **/
  // componentsRouter,
  // chartsRouter,
  // nestedRouter,
  // tableRouter,

  {
    path: '/order',
    component: Layout,
    alwaysShow: true,
    redirect: '/order/page',
    name: 'Order',
    meta: {
      title: '订单管理',
      icon: 'el-icon-document'
    },
    children: [
      {
        path: 'page',
        component: () => import('@/views/order/page'),
        name: 'pageOrder',
        meta: { title: '订单列表', icon: 'edit' }
      },
      // {
      //   path: 'list',
      //   component: () => import('@/views/example/list'),
      //   name: 'ArticleList',
      //   meta: { title: '权限管理', icon: 'list' }
      // }
    ]
  },

  {
    path: '/excel',
    component: Layout,
    alwaysShow: true,
    redirect: '/excel/export-excel',
    name: 'Excel',
    meta: {
      title: '数据分析',
      icon: 'el-icon-s-data',
      roles: ['admin', 'editor'],
  },
    children: [
      {
        path: 'export-excel',
        component: () => import('@/views/excel/export-excel'),
        name: 'ExportExcel',
        meta: { title: '销售数据' }
      }
      // {
      //   path: 'export-selected-excel',
      //   component: () => import('@/views/excel/select-excel'),
      //   name: 'SelectExcel',
      //   meta: { title: 'Export Selected' }
      // },
      // {
      //   path: 'export-merge-header',
      //   component: () => import('@/views/excel/merge-header'),
      //   name: 'MergeHeader',
      //   meta: { title: 'Merge Header' }
      // },
      // {
      //   path: 'upload-excel',
      //   component: () => import('@/views/excel/upload-excel'),
      //   name: 'UploadExcel',
      //   meta: { title: 'Upload Excel' }
      // }
    ]
  },
  {
    path: '/property',
    component: Layout,
    redirect: '/property/machineList',
    alwaysShow: true,
    name: 'Property',
    meta: { title: '资产管理', icon: 'el-icon-s-finance' },
    children: [
      {
        path: 'machineList',
        component: () => import('@/views/property/machineList'),
        name: 'MachineList',
        meta: { title: '货柜管理' }
      },
      {
        path: 'channelConfig',
        component: () => import('@/views/property/channelConfig'),
        name: 'ChannelConfig',
        hidden: true,
        meta: { title: '货道管理' }
      },
      {
        path: 'editChannel',
        component: () => import('@/views/property/editChannel'),
        name: 'EditChannel',
        hidden: true,
        meta: { title: '编辑货道' }
      }
    ]
  },
  {
    path: '/coupon',
    component: Layout,
    redirect: '/coupon/page',
    alwaysShow: true,
    name: 'Coupon',
    meta: { title: '营销管理', icon: 'el-icon-coin'},
    children: [
      // {
      //   path: 'download',
      //   component: () => import('@/views/zip/index'),
      //   name: 'ExportZip',
      //   meta: { title: '营销设置' }
      // },
      {
        path: 'page',
        component: () => import('@/views/coupon/page'),
        name: 'Coupon',
        meta: {
          title: '优惠券列表'
        }
      },
      {
        path: 'addCoupon',
        component: () => import('@/views/coupon/addCoupon'),
        name: 'AddCoupon',
        hidden: true,
        meta: {
          title: '新增优惠券',
        }
      },
      {
        path: 'editCoupon',
        component: () => import('@/views/coupon/addCoupon'),
        name: 'EditCoupon',
        hidden: true,
        meta: {
          title: '编辑优惠券',
        }
      },
    ]
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
